
<!DOCTYPE html>
<html>
  <head>
    <title>Beego Framework Tutorials - 2 - Routing Namespace (part 3 of 3)</title>
    <meta charset='utf-8'>
    <script src='static/slides.js'></script>
  </head>

  <body style='display: none'>

    <section class='slides layout-widescreen'>
      
      <article>
        <h1>Beego Framework Tutorials - 2 - Routing Namespace (part 3 of 3)</h1>
        <h3>A Go HTTP framework for developing HTTP application rapidly</h3>
        
        
          <div class="presenter">
            
  
  <p>
    By Lei Cao
  </p>
  

          </div>
        
      </article>
      
  
  
      <article>
      
        <h3>Content</h3>
        
  <ul>
  
    <li><code>namespace</code> in routing system</li>
  
    <li>How to plan and organize routers</li>
  
  </ul>

      
      </article>
  
  
  
      <article>
      
        <h3>namespace</h3>
        
  
  <p>
    The namespace is inspired by sintra and laravel
  </p>
  

  
  <div class="code"><pre>ns := beego.NewNamespace(&#34;/v1&#34;).
    Cond(func(ctx *context.Context) bool {
    return true
}).
    Filter(&#34;before&#34;, auth).
    Get(&#34;/fixed&#34;, func(ctx *context.Context) {
    ctx.WriteString(&#34;/v1/fixed&#34;)
})

beego.AddNamespace(ns)</pre></div>
  

      
      </article>
  
  
  
      <article>
      
        <h3>namespace API</h3>
        
  <ul>
  
    <li>NewNamespace(prefix string)</li>
  
  </ul>

  
  <div class="code"><pre>Create a `namespace` object</pre></div>
  

  <ul>
  
    <li>namespace contains these methods:</li>
  
  </ul>

  <ul>
  
    <li>Cond(cond namespaceCond)  </li>
  
  </ul>

  
  <div class="code"><pre>Set up the `Condition`, if the conditions are satisfied then process the namespace</pre></div>
  

  <ul>
  
    <li>Filter(action string, filter FilterFunc)</li>
  
  </ul>

  
  <div class="code"><pre>`action` decide when to run the filter, it can be &#39;before&#39; or &#39;after&#39;</pre></div>
  

      
      </article>
  
  
  
      <article>
      
        <h3>namespace API</h3>
        
  <ul>
  
    <li>AutoRouter(c ControllerInterface)</li>
  
    <li>AutoPrefix(prefix string, c ControllerInterface)</li>
  
    <li>Get(rootpath string, f FilterFunc)</li>
  
    <li>Post(rootpath string, f FilterFunc)</li>
  
    <li>Delete(rootpath string, f FilterFunc)</li>
  
    <li>Put(rootpath string, f FilterFunc)</li>
  
    <li>Head(rootpath string, f FilterFunc)</li>
  
    <li>Options(rootpath string, f FilterFunc)</li>
  
    <li>Patch(rootpath string, f FilterFunc)</li>
  
    <li>Any(rootpath string, f FilterFunc)</li>
  
    <li>Router(rootpath string, c ControllerInterface, mappingMethods ...string)</li>
  
    <li>Handler(rootpath string, h http.Handler)</li>
  
  </ul>

  
  <div class="code"><pre>All these functioins in normal router you can also use in `namespace`</pre></div>
  

      
      </article>
  
  
  
      <article>
      
        <h3>namespace API</h3>
        
  
  <p>
    Nested namespaces
  </p>
  

  <ul>
  
    <li>Namespace(ns *Namespace)</li>
  
  </ul>

  
  <div class="code"><pre>ns := beego.NewNamespace(&#34;/v1&#34;).
    Namespace(
        beego.NewNamespace(&#34;/shop&#34;).
            Get(&#34;/:id&#34;, func(ctx *context.Context) {
                ctx.Output.Body([]byte(&#34;shopinfo&#34;))
        }),
        beego.NewNamespace(&#34;/order&#34;).
            Get(&#34;/:id&#34;, func(ctx *context.Context) {
                ctx.Output.Body([]byte(&#34;orderinfo&#34;))
        }),
        beego.NewNamespace(&#34;/crm&#34;).
            Get(&#34;/:id&#34;, func(ctx *context.Context) {
                ctx.Output.Body([]byte(&#34;crminfo&#34;))
        }),
    )</pre></div>
  

      
      </article>
  
  
  
      <article>
      
        <h3>Organizing project&#39;s routing</h3>
        
  <ul>
  
    <li>For small project can just set routers in <code>routers/router.go</code></li>
  
    <li>For multi-modules big project, use <code>namespace</code> and register the router while importing the module</li>
  
  </ul>

  
  <div class="code"><pre>beego.AddNamespce(ns)

|-- beeapi
|-- conf
|   `-- app.conf
|-- controllers
|   `-- default.go
|-- main.go
|-- models
|   `-- object.go
|-- routers
|   `-- router.go
`-- tests
    `-- default_test.go</pre></div>
  

      
      </article>
  
  

      <article>
        <h3>Thank you</h1>
        
          <div class="presenter">
            
  
  <p>
    By Lei Cao
  </p>
  
<p class="link"><a href="http://github.com/lei-cao" target="_blank">http://github.com/lei-cao</a></p>
          </div>
        
      </article>

  </body>
  
</html>
